"use strict";
cc._RF.push(module, '27784/tEHNL6Jn7kbTY7vO8', 'Enemy');
// script/game/Enemy.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Constants_1 = require("../libs/Constants");
var GameData_1 = require("../libs/GameData");
var HelperMgr_1 = require("../libs/HelperMgr");
var CameraManager_1 = require("./CameraManager");
var GameManager_1 = require("./GameManager");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var Enemy = /** @class */ (function (_super) {
    __extends(Enemy, _super);
    function Enemy() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.type = 0;
        /**目标节点 */
        _this.target = null;
        /**是否在跳跃 */
        _this._jump = false;
        _this._jumpDir = 1;
        _this._jumpLine = 0;
        _this._jumpPlayerPos = new cc.Vec2(1, 1);
        _this._jumpHeight = 300;
        /**是否被击飞 */
        _this._flyUp = false;
        /**是否正在寻找玩家 */
        _this._chase = false;
        /**自由活动方向 */
        _this._swingDirection = new cc.Vec2(1, 1);
        /**击飞方向 */
        _this._flyUpDirection = new cc.Vec2(1, 1);
        /**是否碰到障碍降落 */
        _this._descent = false;
        /**鱼的大小 */
        _this._scale = 1;
        /**鱼的透明度 */
        _this._opcity = 1;
        _this._jumpTime = 0;
        _this._flyTime = 0;
        return _this;
    }
    Enemy.prototype.onLoad = function () {
        var _this = this;
        //出生时检测一次距离
        this.getDistance();
        //定时随机改变自由活动朝向
        this.schedule(function () {
            _this.changeDirection();
        }, 1);
    };
    Enemy.prototype.onCollisionEnter = function (other) {
        if (other.node.group == 'player') {
            GameManager_1.default.instace.playerIsHit = true;
            var force = GameManager_1.default.instace.forcePercent * Constants_1.default.RodInfo[GameData_1.default.instance.playRod].force;
            this.flyUp(force / 10);
        }
        if (other.node.group == 'barrier') {
            this.descent();
        }
    };
    Enemy.prototype.changeDirection = function () {
        if (this._chase || this._flyUp || this._descent || this._jump) {
            return;
        }
        var offsetX = 0;
        if (this.node.x > cc.winSize.width - 200) {
            offsetX = -HelperMgr_1.default.randAtoB(0, 5);
        }
        else if (this.node.x < 200) {
            offsetX = HelperMgr_1.default.randAtoB(0, 5);
        }
        else {
            offsetX = HelperMgr_1.default.randAtoB(-5, 5);
        }
        var offsetY = 0;
        if (this.node.y < 50) {
            offsetY = HelperMgr_1.default.randAtoB(0, 2);
        }
        else if (this.node.y > GameManager_1.default.instace.ground.y - 50) {
            offsetY = -HelperMgr_1.default.randAtoB(0, 2);
        }
        else {
            offsetY = HelperMgr_1.default.randAtoB(-2, 2);
        }
        this._swingDirection = new cc.Vec2(offsetX, offsetY);
    };
    Enemy.prototype.findTarget = function () {
        if (GameManager_1.default.instace.stoshs.childrenCount > 0) {
            var ran = HelperMgr_1.default.randAtoB(0, GameManager_1.default.instace.stoshs.childrenCount - 1);
            this.target = GameManager_1.default.instace.stoshs.children[ran];
        }
        else {
            this.target = GameManager_1.default.instace.player;
        }
    };
    /**
     * 获取与主角的距离
     * 小于300 跳跃
     * 大于1000 寻找玩家
     */
    Enemy.prototype.getDistance = function () {
        if (this._flyUp || this._descent) {
            return;
        }
        this.findTarget();
        if (this.target) {
            var distance = HelperMgr_1.default.getDistance(this.target.x, this.target.y, this.node.x, this.node.y);
            var distanceHeight = Math.abs(this.node.y - this.target.y);
            if (distance > 1000) {
                this.chase();
            }
            else if (distance < 300 && distanceHeight < 200) {
                if (this.target.group == "player" && Math.random() <= Constants_1.default.EnemyInfo[this.type].playerjumprate) {
                    this.jump();
                }
                else if (this.target.group == "stosh" && Math.random() <= Constants_1.default.EnemyInfo[this.type].stoshjumprate) {
                    this.jump();
                }
                else {
                    this.swing();
                }
            }
            else {
                this.swing();
            }
        }
        else {
            this.swing();
        }
    };
    /**
     * 跳跃
     */
    Enemy.prototype.jump = function () {
        if (this._jump)
            return;
        this._descent = false;
        this._chase = false;
        this._jump = true;
        this._jumpDir = this.target.x > this.node.x ? 1 : -1;
        this._jumpLine = this.node.y;
        this._jumpPlayerPos = new cc.Vec2(this.target.x, this.target.y);
        this._jumpHeight = HelperMgr_1.default.randAtoB(300, 500);
        GameManager_1.default.instace.createSpray(new cc.Vec2(this.node.x, this.node.y));
        // console.log('跳');
    };
    /**
     * 自由活动？
     */
    Enemy.prototype.swing = function () {
        if (!this._jump)
            return;
        this._descent = false;
        this._flyUp = false;
        this._chase = false;
        this._jump = false;
        // console.log('自由活动');
    };
    /**
     * 碰到障碍物死亡直降
     */
    Enemy.prototype.descent = function () {
        if (this._descent)
            return;
        this._descent = true;
        this._flyUp = false;
        this._chase = false;
        this._jump = false;
    };
    /**
     * 寻找玩家
     */
    Enemy.prototype.chase = function () {
        if (this._chase)
            return;
        this._descent = false;
        this._flyUp = false;
        this._chase = true;
        this._jump = false;
    };
    Enemy.prototype.flyUp = function (force) {
        if (this._flyUp)
            return;
        this._descent = false;
        var offsetX = (this.node.x - GameManager_1.default.instace.player.x) * force;
        var offsetY = (this.node.y - GameManager_1.default.instace.player.y) * force;
        this._flyUp = true;
        this._chase = false;
        this._jump = false;
        this._flyUpDirection = new cc.Vec2(offsetX / 50, offsetY / 200);
        if (!CameraManager_1.default.instance.target) {
            CameraManager_1.default.instance.target = this.node;
        }
    };
    /**切换状态 */
    Enemy.prototype.changeState = function () {
        if (this.type == Constants_1.default.EnemyType.duck) {
            var normal = this.node.getChildByName("normal");
            var fly = this.node.getChildByName("fly");
            var stream = this.node.getChildByName("stream");
            normal && (normal.active = !this._jump);
            fly && (fly.active = this._jump);
            if (stream) {
                if (this._flyUp || this._jump || this._descent) {
                    stream.active = false;
                }
                else {
                    stream.active = this._swingDirection.x != 0;
                }
                var scaleX = 0.8 + Math.abs(this._swingDirection.x) / 5;
                stream.scaleX = HelperMgr_1.default.lerp(stream.scaleX, scaleX, 0.2);
            }
        }
    };
    Enemy.prototype.update = function () {
        if (GameManager_1.default.instace.gameIsStart && !GameManager_1.default.instace.gameIsOver) {
            this.changeState();
            if (!this._jump && !this._flyUp && !this._descent) {
                this._opcity = (1 - Math.abs(this.node.y - GameManager_1.default.instace.ground.y) / 300) * 255;
                this._scale = (1 - Math.abs(this.node.y - GameManager_1.default.instace.ground.y) / 300);
                this.node.opacity = this._opcity;
                this.node.scaleY = this._scale;
                if (this.node.x > cc.winSize.width) {
                    this.node.x = cc.winSize.width;
                }
                if (this.node.x < 0) {
                    this.node.x = 0;
                }
                if (this.node.y > Constants_1.default.EnemyInfo[this.type].range.maxY) {
                    this.node.y = Constants_1.default.EnemyInfo[this.type].range.maxY;
                }
                if (this.node.y < Constants_1.default.EnemyInfo[this.type].range.minY) {
                    this.node.y = Constants_1.default.EnemyInfo[this.type].range.minY;
                }
            }
            if (this._descent) {
                this.node.y -= 10;
                this.node.angle = 90 * this.node.scaleX;
                if (this.node.y < GameManager_1.default.instace.ground.y) {
                    GameManager_1.default.instace.playerIsHit = false;
                    CameraManager_1.default.instance.target = null;
                    GameManager_1.default.instace.createSpray(new cc.Vec2(this.node.x, this.node.y));
                    this.node.removeFromParent();
                    this.node.destroy();
                }
            }
            else if (this._flyUp) {
                this._flyTime += 0.1;
                this.node.angle += 5;
                this.node.x += this._flyUpDirection.x;
                this.node.y += this._flyUpDirection.y - this._flyTime;
                GameManager_1.default.instace.gameScore += Constants_1.default.EnemyInfo[this.type].score;
                if (this.node.y < GameManager_1.default.instace.ground.y) {
                    GameManager_1.default.instace.playerIsHit = false;
                    CameraManager_1.default.instance.target = null;
                    this.node.destroy();
                }
            }
            else if (this._jump) {
                /**跳跃 */
                this._jumpTime += 1;
                this.node.opacity = 255;
                this.node.scaleX = -this._scale * this._jumpDir;
                this.node.x += 3 * this._jumpDir;
                this.node.angle = -this.getAngleBetweenPos();
                if (this._jumpTime % 180 == 0) {
                    GameManager_1.default.instace.createSpray(new cc.Vec2(this.node.x, this.node.y));
                    this._jump = false;
                }
                this.node.y = this._jumpLine + Math.sin(this._jumpTime / 180 * Math.PI) * this._jumpHeight;
            }
            else if (this._chase) {
                /**追逐 */
                this.node.angle = 0;
                this._jumpTime = 0;
                this.getDistance();
                this.node.scaleX = this.node.x < this.target.x ? -this._scale : this._scale;
                this.node.x += this.node.x < this.target.x ? this._scale * 3 : -this._scale * 3;
                this.node.y += this.node.y < this.target.y ? this._scale * 3 : -this._scale * 3;
            }
            else {
                /**自由活动时，也需要检测距离 */
                this.node.angle = 0;
                this._jumpTime = 0;
                this.getDistance();
                this.node.scaleX = this._swingDirection.x > 0 ? -this._scale : this._scale;
                this.node.x += this._swingDirection.x;
                this.node.y += this._swingDirection.y;
            }
        }
    };
    Enemy.prototype.getAngleBetweenPos = function () {
        var offsetX = this.node.x - this._jumpPlayerPos.x;
        var offsetY = this.node.y - this._jumpPlayerPos.y;
        var angle = Math.atan2(offsetX, offsetY) / Math.PI * 180;
        // console.log("当前角度",angle)
        return angle;
    };
    __decorate([
        property(Number)
    ], Enemy.prototype, "type", void 0);
    Enemy = __decorate([
        ccclass
    ], Enemy);
    return Enemy;
}(cc.Component));
exports.default = Enemy;

cc._RF.pop();